using System;
using System.Collections.Generic;
using System.Text;
using AntEater.ComponentModel.BuildElements;
using System.Xml.XPath;
using System.Xml;
using AntEater.ComponentModel;

namespace AntEater.Runtime.Services.Transformation
{

    /// <summary>
    /// Provides a transfomer for the PropertyBuildElement.
    /// </summary>
    [BuildElementTransformer(typeof(PropertyBuildElement), XPathNodeType.Element)]
    internal class PropertyTransformer:BuildElementTransformerBase
    {

        /// <summary>
        /// Initializes a new instance of the <see cref="T:PropertyTransformer"/> class.
        /// </summary>
        /// <param name="context">The context.</param>
        public PropertyTransformer(object context)
            : base(context) {
        }

        /// <summary>
        /// Transforms an <c>BuildElement</c> into XML.
        /// </summary>
        /// <param name="buildElement">The build element.</param>
        /// <param name="writer">The writer.</param>
        /// <param name="errors">The errors.</param>
        public override void GetXml(IBuildElement buildElement, XmlWriter writer, List<TransformationError> errors) {
            PropertyBuildElement element = buildElement as PropertyBuildElement;

            if (element != null) {

                BuildElementAttribute buildElementAttribute
                    = ComponentModel.Utilities.GetTypeAttributeFromObject<BuildElementAttribute>(buildElement);

                if (buildElementAttribute != null) {
                    if (!string.IsNullOrEmpty(element.ElementName)) {
                        writer.WriteStartElement(element.ElementName);

                        if (!string.IsNullOrEmpty(element.Condition)) {
                            writer.WriteAttributeString(this.GetBuildElementPropertyName("Condition"), element.Condition);
                        }

                        if (!string.IsNullOrEmpty(element.Value)) {
                            writer.WriteRaw(element.Value);
                        }

                        writer.WriteEndElement();
                    }
                }
                else {
                    errors.Add(new TransformationError("Name may not be empty!", element));
                }
            }
        }

        /// <summary>
        /// Gets an <c>BuildElement</c> from XML.
        /// </summary>
        /// <param name="node">The node.</param>
        /// <param name="buildFileDefinition"></param>
        /// <param name="errors">The errors.</param>
        /// <returns></returns>
        public override IBuildElement GetBuildElement(XPathNavigator node, BuildFileDefinition buildFileDefinition, List<TransformationError> errors) {
            PropertyBuildElement element = buildFileDefinition.CreateNewBuildElement(typeof(PropertyBuildElement)) as PropertyBuildElement;

            element.ElementName = node.Name;
            element.Value = node.InnerXml;

            if (node.MoveToFirstAttribute()) {
                do {
                    if (node.Name.Equals(this.GetBuildElementPropertyName("Condition"))) {
                        element.Condition = node.Value;
                    }
                } while (node.MoveToNextAttribute());
            }
            return element;
        }
    }
}
